﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using Icms.Core.Model;
using Icms.Core.Repository;
using Omu.ValueInjecter;

namespace Icms.Data
{
    public class ReportRepo : IReportRepo
    {
        public string connectionString;

        public ReportRepo(IConnectionFactory connectionFactory)
        {
            connectionString = connectionFactory.GetConnectionString();
        }

        public DataSet GetData(string name, IDictionary<string,object> parameters)
        {
            var dataSet = new DataSet();
            using (var conn = new SqlConnection(connectionString))
            {
                var adapter = new SqlDataAdapter("getReport_" + name, conn);
                adapter.SelectCommand.CommandTimeout = 3600;
                adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
                adapter.SelectCommand.InjectFrom<SetParamsValues>(parameters);

                adapter.Fill(dataSet);
            }
            return dataSet;
        }

        public IEnumerable<T> QueryToList<T>(string query, object parameters) where T : new()
        {
            using (var conn = new SqlConnection(connectionString))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = query;
                    cmd.InjectFrom<SetParamsValues>(parameters);
                    conn.Open();
                    
                    using (var dr = cmd.ExecuteReader())
                        while (dr.Read())
                        {
                            var o = new T();
                            o.InjectFrom<ReaderInjection>(dr);
                            yield return o;
                        }
                }
            }
        }

        public int GetCount(string query, object parameters) 
        {
            using (var conn = new SqlConnection(connectionString))
            {
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = query;
                    cmd.InjectFrom<SetParamsValues>(parameters);
                    conn.Open();

                    using (var dr = cmd.ExecuteReader())
                        while (dr.Read())
                        {
                            return dr.GetInt32(0);
                        }
                }
            }
            return 0;
        }

        public IPageAbleResult<AssignmentHistory> GetAssignmentData(IDictionary<string, object> parameters)
        {
            var result = new PageAbleResult<AssignmentHistory>();
            result.Lst = QueryToList<AssignmentHistory>("dbo.GetAssignmentHistory", parameters);
            result.Count = GetCount("dbo.GetAssignmentHistory_Count", parameters);
            return result;
        }

        public IEnumerable<CaseAssignmentHistory> GetCaseAssignmentData(IDictionary<string, object> parameters)
        {
            return QueryToList<CaseAssignmentHistory>("dbo.GetCaseAssignmentHistory", parameters);
        }
    }
}